﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using YookingBizWQW.Class.Public;
using YookingFileIO.Class;

namespace YookingBizWQW.Class.WxMini
{
    public class clsWebView
    {
        public string SaveWxMiniImageToHDDir(string strB64RequestData)
        {
            string strB64ParsData = "aabbcc";
            string strImageDataArray = "[{null}]";

            #region 先将请求参数和图片大数据各自解析出来

            string[] strArray = strB64RequestData.Split('◆');
            if (strArray.Length >= 2)
            {
                strB64ParsData = strArray[0]; //客户端请求参数，此处无值
                strImageDataArray = HttpUtility.UrlDecode(strArray[1]);
            }

            #endregion

            #region 再将strB64ParsData包含的各种参数一一解析出来

            //先进行解密，然后在进行中文解码，最后得到明文的请求数据
            string strRequestData = clsHandleData.DeUrlRequest(strB64ParsData);

            string strTmpPars = "no value"; //前台临时参数

            //这些参数用★分隔开，客户端与服务器端要一致
            string[] strAryPars = strRequestData.Split('★');
            if (strAryPars.Length >= 1)
            {
                strTmpPars = strAryPars[0];
            }

            #endregion

            try
            {
                //将文本转成json对象
                JArray aryImgData = JArray.Parse(strImageDataArray);
                for (int i = 0; i < aryImgData.Count; i++)
                {
                    //文件名和纯BASE64数据
                    string strImgFileName = aryImgData[i]["ImgFileName"].ToString();
                    string strImgB64Data = aryImgData[i]["ImgB64Data"].ToString();

                    //将base64文本转成byte数组
                    byte[] byteData = Convert.FromBase64String(strImgB64Data);

                    clsRWFile myFile = new clsRWFile(); //将图像保存到指定目录里面
                    myFile.SaveBinaryFile("/TmpFile/WxWebview", strImgFileName, byteData);
                }

                return "success"; //保存操作顺利完成
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }



    }
}
